home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 8 / FM Towns Free Software Collection 8.iso / t_os / artemis / artsrc1 / spray.c < prev    next >
C/C++ Source or Header  |  1994-06-01  |  5KB  |  256 lines

  1. /*
  2.     spray.c
  3.  
  4.     spray_init        モジュールの初期化
  5.     commandSpray
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <math.h>
  13.  
  14. #include "ge.h"
  15. #include "dispman.h"
  16. #include "imageman.h"
  17.  
  18. #if 0
  19.     
  20.     static char *sprayPat[] =
  21.     {
  22.         "000001111100000",
  23.         "000111111111000",
  24.         "001111222111100",
  25.         "011122222221110",
  26.         "011222444222110",
  27.         "111224666422111",
  28.         "1122468b8642211",
  29.         "112246bbb642211",
  30.         "1122468b8642211",
  31.         "111224666422111",
  32.         "011222444222110",
  33.         "011122222221110",
  34.         "001111222111100",
  35.         "000111111111000",
  36.         "000001111100000",
  37.         NULL
  38.     };
  39.     
  40.     
  41.     static int sprdatnum = 0;
  42.     static struct {signed char dx,dy} sprdat[16*16*16], *sprp;
  43.     
  44.     
  45.     static int ctox(char c)
  46.     {
  47.         if ('0'<=c && c<='9')
  48.             return c-'0';
  49.         else if ('a'<=c && c<='f')
  50.             return c-'a'+10;
  51.         else if ('A'<=c && c<='F')
  52.             return c-'A'+10;
  53.         else
  54.             return 0;
  55.     }
  56.     
  57.     static void makeSprayData(char **data)
  58.     {
  59.         int xlen,ylen;
  60.         xlen=0;
  61.         for (ylen=0; *(data+ylen)!=NULL; ylen++)
  62.             xlen = _max(strlen(*(data+ylen)),xlen);
  63.         int i,j,k;
  64.         sprdatnum=0;
  65.         for (i=0; i<ylen; i++) {
  66.             char *p;
  67.             p = *(data+i);
  68.             for (j=0; j<xlen && *(p+j)!=0; j++,p++) {
  69.                 for (k = ctox(*p);  k;  k--) {
  70.                     sprdat[sprdatnum].dx = j-xlen/2;
  71.                     sprdat[sprdatnum].dy = i-ylen/2;
  72.                     sprdatnum++;
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78. #endif
  79.  
  80. /*--------------------------------------------------------*/
  81. /*                   モジュールの初期化                   */
  82. /*--------------------------------------------------------*/
  83.  
  84. typedef    int    ireal;
  85.  
  86. #if 0
  87.     static ireal *sintbl = NULL, *costbl = NULL;
  88. #endif
  89.  
  90. #define    ID(n)    ((n)<<10)
  91. #define    FD(n)    (int)((n)*1024.0)
  92. #define    DI(n)    (((n)+512)>>10)
  93. #define    ANGLESTEP    1024        /* 2πを何分割するか */
  94.  
  95. int spray_init(void)
  96. {
  97. #if 0
  98.     if (sintbl == NULL)
  99.     {
  100.         if ((sintbl = (ireal*)malloc(sizeof(ireal)*((ANGLESTEP/8)+1)*2))
  101.             == NULL)
  102.             return -1;
  103.         costbl = sintbl + ((ANGLESTEP/8)+1);
  104.         int i;
  105.         for (i=0; i<=ANGLESTEP/8; i++)
  106.         {
  107.             sintbl[i] = FD(sin(2*_PI*(double)i/ANGLESTEP));
  108.             costbl[i] = FD(cos(2*_PI*(double)i/ANGLESTEP));
  109.         }
  110.     }
  111. #endif
  112.     return 0;
  113. }
  114.  
  115. #if 0
  116. void getsincos(int theta, ireal *sin, ireal *cos)
  117. // theta : 0..ANGLESTEP の整数
  118. {
  119.     while (theta < 0)
  120.         theta += ANGLESTEP;
  121.     theta %= ANGLESTEP;
  122.     int a,b,c,si,co;
  123.     a = theta / (ANGLESTEP/4);            // a = 0,1,2,3 : 象限を表す
  124.     b = theta % (ANGLESTEP/4);
  125.     if (b > (ANGLESTEP/8))
  126.         c = 1, b = ANGLESTEP/4 - b;
  127.     else
  128.         c = 0;
  129.     if (c == 0)
  130.         si = sintbl[b], co = costbl[b];
  131.     else
  132.         si = costbl[b], co = sintbl[b];
  133.     switch(a)
  134.     {
  135.     case 1:        *sin = si, *cos = -co;    break;
  136.     case 2:        *sin = -si, *cos = -co;    break;
  137.     case 3:        *sin = -si, *cos = co;    break;
  138.     default:    *sin = si, *cos = co;    break;
  139.     }
  140. }
  141. #endif
  142.  
  143. /*--------------------------------------------------------*/
  144. /*                   スプレーのプロット                   */
  145. /*--------------------------------------------------------*/
  146.  
  147. static void xpset(int x,int y,int col)
  148. {
  149.     int col0,r,g,b,r0,g0,b0,rnd,i,rr,gr,br;
  150.     col0 = EIMpoint(x,y);
  151.     r = getR(col);        g = getG(col);        b = getB(col);
  152.     r0 = getR(col0);    g0 = getG(col0);    b0 = getB(col0);
  153.     for (i=0; i<2; i++) {
  154.         rr = _abs(r-r0);    gr = _abs(g-g0);    br = _abs(b-b0);
  155.         if (rr+gr+br == 0)
  156.             break;
  157.         rnd = rand() % (rr+gr+br);
  158.         if (rnd < rr) {
  159.             if (r > r0)            r0++;
  160.             else if (r < r0)    r0--;
  161.         } else if (rnd < rr+gr) {
  162.             if (g > g0)            g0++;
  163.             else if (g < g0)    g0--;
  164.         } else {
  165.             if (b > b0)            b0++;
  166.             else if (b < b0)    b0--;
  167.         }
  168.     }
  169.     EIMpset(x,y,GRB(g0,r0,b0),DrawNORMAL);
  170. }
  171.  
  172.  
  173. static void plot_spray(int x, int y, int r, int col)
  174. {
  175.     int mixr0,mixr;
  176.     mixr0 = getmixrate();
  177.     for (int i=0; i<10; i++) {
  178.         int dx,dy;
  179.         dx = rand() % (spray_r*2+1) - spray_r;
  180.         dy = rand() % (spray_r*2+1) - spray_r;
  181.         if (dx*dx+dy*dy > spray_r*spray_r)
  182.             continue;
  183.         int xx,yy;
  184.         xx = x + dx;        yy = y + dy;
  185.         if (0<=xx && xx<EIMgetxsize() && 0<=yy && yy<EIMgetysize())
  186.         {
  187.             if (spray_t == 0)
  188.             {
  189.                 if (mixr0 == 256)
  190.                     xpset(xx,yy,col);
  191.                     // EIMgraypset(xx,yy,col,7);
  192.                 else
  193.                 {
  194.                     char *cp = cbuf_adrs(xx,yy);  int conc = 32;
  195.                     int t = cbuf_mix(cbuf2conc(*cp), conc, mixr0);
  196.                     *cp = conc2cbuf(t);
  197.                     EIMpset(xx,yy,
  198.                             mixcol(EIMpoint_back(xx,yy),col,t,NO),
  199.                             DrawNORMAL);
  200.                 }
  201.             }
  202.             else
  203.                 psetWithPen(xx,yy,col,getcurpen(),YES);
  204.         }
  205.     }
  206. }
  207.  
  208. /*--------------------------------------------------------*/
  209. /*                 スプレーコマンドの実行                 */
  210. /*--------------------------------------------------------*/
  211.  
  212. static void spray()
  213. {
  214.     int x,y;
  215.     x = DMimage_getx(ms.x);
  216.     y = DMimage_gety(ms.y);
  217.     int i;
  218.     int xx,yy;
  219.     plot_spray(x, y, 16, forecol);
  220. }
  221.  
  222.  
  223. void commandSpray()
  224. {
  225.     DEBUG_MSG("commandSpray begin");
  226.     // sprintf(debugmsg, "scrmode: %d", scrmode);
  227.     DEBUG_MSG(debugmsg);
  228.     bool first;
  229.     // if (sprdatnum == 0)
  230.     //     makeSprayData(sprayPat);
  231.     first = YES;
  232.     for (;;) {
  233.         DMdispcsr(ms.x,ms.y);
  234.         do {
  235.             ms_get(&ms);
  236.         } while (ms.dx==0 && ms.dy==0 && ms.btn1==OFF && ms.btn2==OFF &&
  237.                  key_chk() == 0);
  238.         DMerasecsr();
  239.         scrollForCsr(1,1);
  240.         if (ms.btn1 == OFFON) {
  241.             first = NO;
  242.             EIMbackup();
  243.             cbuf_clear();
  244.             spray();
  245.         } else if (ms.btn1 == ON) {
  246.             if (!first)
  247.                 spray();
  248.         }
  249.         if (ms.btn2 == OFFON)
  250.             break;
  251.     }
  252.     DEBUG_MSG("commandSpray end");
  253. }
  254.  
  255. // end of spray.c
  256.